﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WorkBasket.Data.Entities;
using WorkBasket.Data.Repository;
using WorkBasket.Infrastructure;
using WorkBasket.Models;
 

namespace WorkBasket.Controllers.Api
{
    [Authorize]
    public class FieldController : ApiController
    {
        private SOC soc = new SOC(new MyContext());
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        [Route("api/field/GetByParentKey/{WorkflowID}")]
        [HttpGet]
        public IHttpActionResult GetByParentKey(int WorkflowID)
        {
            var soc = new SOC(new MyContext());
            return Json(soc.Fields.Where(c => c.WorkflowID == WorkflowID));
        }

        public string Get(int id)
        {
            return "value";
        }

        [HttpPost]
        [Route("api/field/save")]
        public IHttpActionResult Post([FromBody] Field value)
        {
            try
            {
                if (value.FieldID == 0)
                {
                    soc.Fields.Add(value);
                }
                else
                {
                    soc.Fields.Update(value);
                }

                soc.CommitChanges();
                return Json(new DataReturn("Field Saved", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }
        public void Put(int id, [FromBody] string value)
        {
        }

        [HttpPost]
        [Route("api/field/delete")]
        public IHttpActionResult Delete([FromBody] Field value)
        {
            try
            {
                soc.Fields.Delete(value.FieldID);
                soc.CommitChanges();
                return Json(new DataReturn("Field Deleted", value));
            }
            catch (Exception e)
            {
                return new System.Web.Http.Results.ExceptionResult(e, this);
            }
        }
    }
}
